package com.minivision.sdktestdemo.ui.view;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.event.PlayMusicEvent;
import com.minivision.sdktestdemo.theme.utils.ThemeUtils;

import org.greenrobot.eventbus.EventBus;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/6/6.
 */
public class LockPopContentView implements PopContentView,LockPopupView.LockContentView {
    private Activity mContext;

    private View mContentView;

    private PopupWindow mPopupWindow;

    @Bind(R.id.lockScreen_date)
    TextView mTvData;

    @Bind(R.id.lockScreen_time)
    TextView mTvTime;

    @Bind(R.id.lockScreen_musicName)
    TextView mTvMusicName;

    @Bind(R.id.lockScreen_singer)
    TextView mTvSinger;

    @Bind(R.id.btn_play)
    ImageButton mBtnPlay;

    @Bind(R.id.icon_slider)
    ImageView mSlider;

    @Bind(R.id.layout_lock)
    RelativeLayout mLayout;

    @Bind(R.id.img_back)
    SimpleDraweeView mBackImg;

    private AnimationDrawable mAnim;

    private PlayMusicEvent mEvent;

    private Timer mTimer;

    private String mTimeText = "";

    private String mDateText = "";

    private Calendar mCal;

    public LockPopContentView(Activity context) {
        mContext = context;
        ButterKnife.bind(this, getContentView());
        mCal = Calendar.getInstance();
        mAnim = (AnimationDrawable) mSlider.getDrawable();
        mDateFormat = new SimpleDateFormat("HH:mm");
    }

    @Override
    public int getMaxPopWindowHeight() {
        return ViewGroup.LayoutParams.MATCH_PARENT;
    }

    @Override
    public View getContentView() {
        if (mContentView == null) {
            mContentView = mContext.getLayoutInflater().inflate(R.layout.pop_lock_view, null);
        }
        return mContentView;
    }

    @Override
    public void dismiss() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }
    }

    @Override
    public void setPopupWindow(PopupWindow popupWindow) {
        mPopupWindow = popupWindow;
    }

    @Override
    public Activity getContext() {
        return mContext;
    }

    private boolean isStarted;

    @Override
    public void startShowTime() {
        mTimer = new Timer(true);
        mAnim.start();
        isStarted = true;
        mTimer.schedule(new TimerTask() {
            private Date mDate = new Date();

            @Override
            public void run() {
                mDate.setTime(System.currentTimeMillis());
                final String dateText = getDateText(mDate);
                final String timeText = getTimeText(mDate);
                if (!(dateText.equals(mDateText) && timeText.equals(mTimeText))) {
                    mContext.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mTvTime.setText(timeText);
                            mTvData.setText(dateText);
                        }
                    });
                }
            }
        }, 0, 1000);
    }

    @Override
    public void release() {
        if (isStarted) {
            mTimer.cancel();
            isStarted = false;
        }
    }

    private String getDateText(Date date) {
        mCal.setTime(date);
        mCal.get(Calendar.DAY_OF_WEEK);
        String text = (mCal.get(Calendar.MONTH) + 1) + "月" + mCal.get(Calendar.DAY_OF_MONTH) + "日 " + texts[mCal.get(Calendar.DAY_OF_WEEK) - 1];
        return text;
    }

    private String texts[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    private DateFormat mDateFormat;

    private String getTimeText(Date date) {
        mCal.setTime(date);
        String text = mDateFormat.format(date);
        return mDateFormat.format(date);
    }

    @OnClick(R.id.btn_play)
    void onPlay() {
        mEvent.mStatus = mEvent.mStatus == PlayMusicEvent.PLAY ? PlayMusicEvent.PAUSE : PlayMusicEvent.PLAY;
        mBtnPlay.setImageResource(mEvent.mStatus == PlayMusicEvent.PLAY ? R.drawable.lock_btn_pause : R.drawable.lock_btn_play);
        EventBus.getDefault().post(mEvent);
    }

    @OnClick(R.id.btn_prev)
    void onPrev() {
        mEvent.mStatus = PlayMusicEvent.PREV;
        EventBus.getDefault().post(mEvent);
    }

    @OnClick(R.id.btn_next)
    void onNext() {
        mEvent.mStatus = PlayMusicEvent.NEXT;
        EventBus.getDefault().post(mEvent);
    }

    @Override
    public void updateEvent(PlayMusicEvent event) {
        setPlayEvent(event);
        mTvMusicName.setText(event.mName);
        mTvSinger.setText(event.mSubName);
        mBtnPlay.setImageResource(event.mStatus == PlayMusicEvent.PLAY ? R.drawable.lock_btn_pause : R.drawable.lock_btn_play);
        mBackImg.setImageURI(event.mAlbumUri);
    }

    private void setPlayEvent(PlayMusicEvent event) {
        if (mEvent == null) {
            mEvent = event;
        } else {
            mEvent.setPlayEvent(event);
        }
    }
}
